﻿using AutoMapper;
using Modules.Entitys;
using Modules.Notify;
using Modules.System.Attachment;
using PmSoft;
using System.ComponentModel.DataAnnotations;
using static Modules.Notify.NotifyConstants;

namespace ManagementApi.Models.Notify
{
    /// <summary>
    /// 系统通知展示字段
    /// </summary>
    public class SysNotifyDto
    {
        /// <summary>
        /// 系统通知Id
        ///</summary>
        public string Id { get; set; }

        /// <summary>
        /// 发布内容
        ///</summary>
        public string? Content { get; set; }

        /// <summary>
        /// 限制查看人类型(1：所有人可见 2：建设单位可见 3：总承包可见 4：专业分包可见 5：劳务公司可见)
        ///</summary>
        public 可见对象类型 LimitReaderType { get; set; }

        /// <summary>
        /// 是否置顶该通知
        /// </summary>
        public bool IsTop { get; set; }

        /// <summary>
        /// 限制查看人类型(1：所有人可见 2：建设单位可见 3：总承包可见 4：专业分包可见 5：劳务公司可见)
        ///</summary>
        public string? LimitReaderTypeName { get; set; }

        /// <summary>
        /// 已读次数
        ///</summary>
        public int? ReadedNum { get; set; }

        /// <summary>
        /// 发布者
        ///</summary>
        public string? Creator { get; set; }

        /// <summary>
        /// 发布时间
        ///</summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 修改时间
        ///</summary>
        public DateTime? UpdateTime { get; set; }

        /// <summary>
        /// 附件集合
        /// </summary>
        public IEnumerable<AttachmentInfo>? attachments { get; set; }
    }



    /// <summary>
    /// 扩展字段处理
    /// </summary>
    public static partial class SysNotifyExceptions
    {
        /// <summary>
        /// 转换Dto
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static SysNotifyDto AsSysNotifyDto(this SysNotify entity)
        {
            SysNotifyDto sysNotifyDto = new SysNotifyDto();
            var mapper = ServiceAccessor.GetService<IMapper>();
            var notifyAttachmentService = ServiceAccessor.GetService<NotifyAttachmentService>();
            mapper.Map(entity, sysNotifyDto);
            sysNotifyDto.LimitReaderTypeName = sysNotifyDto.LimitReaderType.GetDescription();
            sysNotifyDto.attachments = notifyAttachmentService.GetSysAttachmentByNotifyId(entity.Id);
            return sysNotifyDto;
        }

        public static IEnumerable<SysNotifyDto> AsSysNotifyDtos(this IEnumerable<SysNotify> entitys)
        {
            List<SysNotifyDto> dtos = new List<SysNotifyDto>();
            foreach (var entity in entitys)
            {
                dtos.Add(entity.AsSysNotifyDto());
            }
            return dtos;
        }

    }
}